/*
 * =====================================================================================
 *
 *       Filename:  mystring.c
 *
 *    Description:  implement some function in C library 
 *
 *        Version:  1.0
 *        Created:  2012年02月24日 20时16分04秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Regan (), lcqhigh@gmail.com
 *        Company:  
 *
 * =====================================================================================
 */

//#include "mystring.h"	
typedef unsigned int size_t;

#define	NULL 0			/*  */

void* Memchr(const void *s, int c, size_t n)
{
    const unsigned char uc = c;
    const unsigned char *su = s;

    
    for (su = s; 0 < n; ++su, --n) 
    {
        if (*su == uc)
            return ((void*)su);
    }
    return (NULL);
}

int Memcmp(const void *s1, const void *s2, size_t n)
{
    const unsigned char *us1 = s1;
    const unsigned char *us2 = s2;
    
    for (; 0 < n; ++us1, ++us2, --n)
    {
        if (*us1 != *us2)
        {
            return ((*us1 < *us2) ? -1 : +1);
        }
    }
    return 0;   
}

void *Memcpy(void *dst, const void *src, size_t n)
{
    char *s1;
    const char *s2;

    for (s1 = dst, s2 = src; 0 < n; ++s1, ++s2, --n) {
        *s1 = *s2;
    }
    //side effect blew code
//    while (n--) {
//        *s1++ = *s2++;
//    }
    return (s1);
}

//向量上，取数据的方向与复制数据的方向相反
void *Memmove(void *dst, const void *src, size_t n)
{
   char *s1 = dst;
   const char *s2 = src;
   //copy the element in desc order
   if (s2 < s1 && (s1 < s2 + n))
   {
       for (s1 += n, s2 +=n; 0 < n; --n)
           *--s1 = *--s2;
   }
   else 
   {
       for (; 0 < n; --n)
           *s1++ = *s2++;
   }
   return (s1);
}

void *Memset(void *s, int c, size_t n)
{
    const unsigned char uc = c;
    unsigned char *su = s;

    for ( ; 0 < n ; ++su, --n )
        *su = uc;
    return (s);
}

char* Strncat(char *dst, const char *src, size_t n)
{
    char *s = dst;
    while (*s) {
        ++s;
    }

    const char *s2 = src;
    for (; 0 < n && *s2 != '\0'; --n)
    {
        *s++ = *s2++;
    }
    *s = '\0';
    return (dst);
}

int Strncmp(const char *s1, const char *s2, size_t n)
{
    const unsigned char *su1 = (const unsigned char*)s1;
    const unsigned char *su2 = (const unsigned char*)s2;
    for ( ; 0 < n;++su1, ++su2, --n) {
        if (*su1 != *su2)
        {
            if (*su1 > *su2)
                return 1;
            return -1;
        }
        //very good
        else if (*su1 == '\0')
            return  0;
    }
    return 0; 
}

int Strcmp(const char *s1, const char *s2)
{

    for (; *s1 == *s2; ++s1, ++s2) {
        if (*s1 == '\0')
            return (0);
    }

    return ((*(unsigned char*)s1 < *(unsigned char*)s2) ? -1 : 1);
}

char *Strncpy(char *dst, const char *src, size_t n)
{
    char *s = dst;

    for (; 0 < n && *src != '\0'; --n)
        *s++ = *src++;
    for (; 0 < n; --n)
        *s++ = '\0';
    return dst;
}

char *Strcpy(char *dst, const char *src)
{
    char *s = dst;

    for (; *src != '\0'; ++src)
        *s++ = *src;

    return (dst);
}

char *Strcat(char *dst, const char *src)
{
    char *s = dst;
    while (*s != '\0')
    {
        ++s;
    }

    for (; *src != '\0'; ++src)
        *s++ = *src;
    return dst;
}

size_t Strlen(const char *s)
{
    const char *sc;
    for (sc = s; *sc != '\0'; ++sc)
        ;
    return (sc -s);
}

//往往忽略了空字符结尾这个条件
char *Strchr(const char *s, int c)
{
    const char ch = c;

    for (; *s != ch ; ++s)
        if (*s != '\0')
            return NULL;
    return (char*)s;
}


